﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Text;

namespace EXAM
{
    public partial class Login : System.Web.UI.Page
    {
        String sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["UserGuest"] = null;
                LoadInformation();
                Session.Clear();
                SaveResult("dsTestLog");
                Page.Master.FindControl("linkUser").Visible = false;
                ((HtmlGenericControl)Page.Master.FindControl("linkLogin")).InnerHtml = ((HtmlGenericControl)Page.Master.FindControl("linkLogin")).InnerHtml.Replace("Log out", "Log in");
            }
        }
        private void SaveResult(string _dsetName)
        {
            if (Session["FlagTest"] != null)
            {
                DataSet dset = Session[_dsetName] as DataSet;

                int index = (int)Session["QuestIndex"];
                BUS.Test_LogControl logCtr = new BUS.Test_LogControl();
                long idTest = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["Test_ID"]);
                long idUrs = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["User_ID"]);
                string type = Convert.ToString(dset.Tables["dtQuest"].Rows[0]["Type"]);
                if (type == "TMP")
                    sErr = logCtr.Delete(idTest, idUrs, type);
                BUS.Test_Log_AnswerControl ansCtr = new BUS.Test_Log_AnswerControl();
                int No = logCtr.GetMaxNo() + 1;
                foreach (DataRow row in dset.Tables["dtQuest"].Rows)
                {
                    DTO.Test_LogInfo logInf = new DTO.Test_LogInfo(row);
                    DTO.UserInfo user = Session["UserGuest"] as DTO.UserInfo;
                    if (user == null)
                        Response.Redirect("Login.aspx?hander=save");
                    DataRow[] rows = dset.Tables["dtAnswer"].Select("Test_Log_No = " + logInf.No + " and Test_Log_Line = " + logInf.Line);
                    for (int i = rows.Length - 1; i >= 0; i--)
                    {
                        DataRow rowA = rows[i];
                        DTO.Test_Log_AnswerInfo aInf = new DTO.Test_Log_AnswerInfo(rowA);
                        aInf.Test_Log_No = No;
                        ansCtr.Add(aInf, ref sErr);
                        dset.Tables["dtAnswer"].Rows.Remove(rowA);
                    }
                    logInf.User_ID = user.ID;
                    logInf.No = No;
                    //logInf.Type = "";
                    logCtr.Add(logInf, ref sErr);

                }
            }
            if (sErr == "")
            {
                Session["QuestIndex"] = null;
                Session[_dsetName] = null;
                Session["Duration_Time"] = null;
                Session["CountAns"] = null;
                Session["FlagTest"] = null;

            }

        }
        public string GetDayOfWeek(string val)
        {
            if (val == null || val == "")
                return "";
            int ival = Convert.ToInt32(val);
            BUS.CommonControl ctr = new BUS.CommonControl();
            DateTime time = BUS.CommonControl.ConvertIntToDate(ival).Value;
            return time.DayOfWeek.ToString();

        }
        public string FormatPercent(string val)
        {
            if (val == null || val == "")
                return "";
            double dval = Convert.ToDouble(val);
            return dval.ToString("N2") + "%";
        }

        public string GetTimeOnly(string val)
        {
            if (val == null || val == "")
                return "";
            int ival = Convert.ToInt32(val);
            int min = ival / 60;
            int secs = ival - min * 60;
            return "Minutes " + min + ":" + secs.ToString("00");
        }
        public string GetDateOnly(string val)
        {
            if (val == null || val == "")
                return "";
            int ival = Convert.ToInt32(val);
            BUS.CommonControl ctr = new BUS.CommonControl();
            DateTime time = BUS.CommonControl.ConvertIntToDate(ival).Value;
            return time.DayOfWeek.ToString().Substring(0, 3) + "," + time.ToString("MMM dd, yy");
        }
        private void LoadInformation()
        {
            BUS.CommonControl ctr = new BUS.CommonControl();
            string query = "exec procTest_getTopInfo";
            DataTable dt = ctr.executeSelectQuery(query);
            RadRotator1.DataSource = dt;
            RadRotator1.DataBind();

        }

        protected void btLogin_Click(object sender, EventArgs e)
        {
            BUS.UserControl usrctr = new BUS.UserControl();
            DTO.UserInfo usrinf = new DTO.UserInfo();
            usrinf = usrctr.Get_User_ByUsername(txtUsername.Text.Trim(), ref sErr);
            String passcod = Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(txtPassword.Text)));
            if (usrinf.ID > 0 && usrinf.Password == passcod)
            {
                if (usrinf.ANAL_U8 == "Y" || usrinf.ANAL_U8 == "")
                {
                    Session["UserGuest"] = usrinf;

                    if (Request["hander"] != null)
                    {
                        if (Request["hander"].ToString() == "save")
                            SaveResult("dsTestLog");
                    }
                    Session.Clear();
                    Session["UserGuest"] = usrinf;
                    BUS.UserLogControl ctr = new BUS.UserLogControl();
                    DTO.UserLogInfo inf = new DTO.UserLogInfo(-1, usrinf.ID, DateTime.Now, usrinf.Username);
                    ctr.Add(inf, ref sErr);

                    Response.Redirect("news.aspx");
                }
                else lbErr.Text = "User is disabled";
            }
            else
            {
                lbErr.Text = "Not correct !!!";
            }
        }

    }
}
